const GU = require('../gameUtils.js');
const {ACTION_CODE, CARD, PLAYER_STATE} = require('./chudadiConstants.js');
// const checkHu = require('./checkHu.js');

var ChudadiPlayer = function (user) {
    this.id = user.id;
    this.socket = user.socket;
    this.dirty = new Set;
};

p = ChudadiPlayer.prototype;
// 重置数据
p._resetData = function () {
    this.actionCode = 0;
    this.handCards = [];
    this.playedCards = [];
    this.playingCards = [];
    this.dirty.clear(); // 记录那些量被更新了
};

/**
 * 获取玩家数据
 * @param {Boolean} needMask - 是否需要屏蔽（对别人不可见的牌）
 * @param {Boolean} incremental - 是否增量（根据this.dirty列表）
 */
p.getData = function (needMask, incremental) {
    let res = {}; // id: this.id
    if (!incremental || this.dirty.has(PLAYER_STATE.ACTION_CODE)) res.actionCode = needMask ? ACTION_CODE.None : this.actionCode;
    if (!incremental || this.dirty.has(PLAYER_STATE.HAND_CARDS)) res.handCards = needMask ? this.handCards.concat().fill(CARD.NONE) : this.handCards;
    if (!incremental || this.dirty.has(PLAYER_STATE.PLAYED_CARDS)) res.playedCards = incremental ? this.playedCards[this.playedCards.length-1] : this.playedCards;
    if (!incremental || this.dirty.has(PLAYER_STATE.PLAYING_CARD)) res.playingCards = this.playingCards;
    return Object.keys(res).length > 0 ? res : undefined;
};
// 清除玩家动作数据（包括actionCode、chiList、gangList）
p.clearActionData = function () {
    this.setActionCode(0);
};
// setter
p.setActionCode = function (actionCode) {
    if (this.actionCode != actionCode) {
        this.actionCode = actionCode;
        this.dirty.add(PLAYER_STATE.ACTION_CODE);
    }
}

// 玩家动作
/**
 * 玩家打出一组牌
 * @param {Number} cardIndexes - 卡牌序数数组
 */
p.playCard = function (cardIndexes) {
    cardIndexes.sort((a, b) => a - b)
    const playingCards = cardIndexes.map(index => this.handCards[index])
    for (let i = cardIndexes.length; i--;) {
        this.handCards.splice(cardIndexes[i], 1);
    }
    this.dirty.add(PLAYER_STATE.HAND_CARDS);
    this._setPlayingCards(playingCards); // 设置打出的牌
};
// 打出牌后，没有人吃碰杠胡，则将打出牌放置于弃牌区
p.playCardEnd = function () {
    this.playedCards.push(this.playingCards);
    this._setPlayingCards([]);
    this.dirty.add(PLAYER_STATE.PLAYED_CARDS);
    this.dirty.add(PLAYER_STATE.PLAYING_CARD);
};

// 动作执行合法性判断
// 判胡
p.canHu = function (card) {
    // return checkHu(this, this.config, card);
    return true
};

// TODO 判断玩家是否存在某些indexes的卡牌
p.hasCardIndexes = function (cardIndexes) {
    const len = this.handCards.length
    return cardIndexes.every(index => 0 <= index && index < len)
};
// 整理手牌（如有newCard，则将newCard也整理进手牌中）
// TODO: 效率有待改进
p.sortHandCard = function (clone = false) {
    let res = clone ? this.handCards.concat() : this.handCards;
    !clone && this.dirty.add(PLAYER_STATE.HAND_CARDS); // 如果不是获取复制数据，则手牌数据被改变
    res.sort((a, b) => a - b);
    return res;
};
// 获取所有手牌
p._getAllHandCards = function (sort = false) {
    if (sort) {
        return p.sortHandCard(true);
    } else {
        let allHandCards = [].concat(this.handCards);
        return allHandCards;
    }
};
// 私有setter
p._setPlayingCards = function (cards) {
    this.playingCards = cards;
    this.dirty.add(PLAYER_STATE.PLAYING_CARD);
};
module.exports = ChudadiPlayer;